package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;

import com.google.gson.Gson;

import net.wsfcp.android.BuildConfig;
import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqLoginBean;
import net.wsfcp.android.bean.resultbean.PublicKeyBean;
import net.wsfcp.android.bean.resultbean.UserBean;
import net.wsfcp.android.http.UrlHelper;
import net.wsfcp.android.http.exception.ApiException;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;

import okhttp3.MediaType;
import okhttp3.RequestBody;

//侧边栏菜单
public class MyModel extends BaseModel {

    public void getPublicKey(@NonNull final PublicKeyHint infoHint) {
        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        httpService.getPublicKey().compose(new CommonTransformer()).subscribe(new CommonSubscriber<PublicKeyBean>(BaseApplication.getContext()) {
            @Override
            public void accept(PublicKeyBean bean) {
                infoHint.getPublicKeySuccess(bean);
            }

        }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
            @Override
            public void accept(Throwable t) {
                ServerException e = ExceptionEngine.handleException(t);
                infoHint.getPublicKeyFail(e.message);

            }

        });

    }


    public void autoLogin(@NonNull final String username, @NonNull final String pwd, @NonNull final AutoLoginHint infoHint) {
        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");


        ReqLoginBean bean = new ReqLoginBean(username, pwd);
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.login(UrlHelper.LOGIN + UrlHelper.getUserName(),body).compose(new CommonTransformer()).subscribe(new CommonSubscriber<UserBean>(BaseApplication.getContext()) {
            @Override
            public void accept(UserBean bean) {
                infoHint.autoLoginSuccess(bean);
            }

        }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
            @Override
            public void accept(Throwable t) {
                ServerException e = ExceptionEngine.handleException(t);
                    infoHint.autoLoginFail(e.message);
            }
        });

    }


    public interface PublicKeyHint {
        //获取侧边栏菜单回调
        void getPublicKeySuccess(PublicKeyBean publicKeyBean);

        void getPublicKeyFail(String str);

    }


    public interface AutoLoginHint {
        //自动登陆回调
        void autoLoginSuccess(UserBean bean);

        void autoLoginFail(String str);


    }

}
